<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\IFrameGrid;
use Dcat\Admin\Models\Administrator as AdministratorModel;
use App\Models\AdminUser;
use App\Models\Tjrlog;


class SaveRecommendController extends BaseController
{

    public function title()
    {
        return trans('admin-user.labels.admin_user');
    }

    protected function grid()
    {
        return Grid::make(new AdminUser(), function (Grid $grid) {
            $grid->tableCollapse(false);
            $grid->responsive();
            $grid->setActionClass(Grid\Displayers\ContextMenuActions::class);
            $grid->model()->orderBy('id', 'desc');
            $grid->model()->where(function ($query) {
                $query->whereHas('roles', function ($q) {
                    $q->where('id', 2);
                });
            });

            $grid->id('标识')->sortable();
            $grid->name(trans('admin-user.fields.name'));
            $grid->en_name(trans('admin-user.fields.en_name'));
            $grid->tjr_id(trans('admin-user.fields.tjr_id'))->display(function ($v) {
                return AdminUser::where('id', $v)->first()->name ?? '公司帐户';
            })->label('success');
            $grid->username(trans('admin-user.fields.username'));
            $grid->mt_login(trans('admin-user.fields.mt_login'));
            $grid->created_at;

            // 按钮
            //$grid->batchActions(function ($batch){
            //$batch->disableDelete();
            //});
            $grid->quickSearch(['id', 'name', 'username']);
            $grid->showQuickEditButton();
            $grid->disableCreateButton();


            $grid->actions(function (Grid\Displayers\Actions $actions) use ($grid) {
                $actions->disableEdit();
                $actions->disableDelete();
                $actions->disableView();

                if ($actions->getKey() == 3) {
                    $grid->disableActions();
                    $actions->disableDelete();
                    $actions->disableQuickEdit();
                }
            });
        });
    }


    protected function iFrameGrid()
    {
        $grid = new IFrameGrid(new AdminUser());

        $grid->quickSearch(['id', 'name', 'username']);

        $grid->id->sortable();
        $grid->username;
        $grid->name;
        $grid->created_at;

        return $grid;
    }


    public function form()
    {
        return Form::make(new AdminUser(['roles']), function (Form $form) {
            $userTable  = config('admin.database.users_table');
            $connection = config('admin.database.connection');
            $id         = $form->getKey();
            $form       = $this->formButton($form);
            $form->text('username', trans('admin.username'))
                ->creationRules(['required', "unique:{$connection}.{$userTable}"])
                ->updateRules(['required', "unique:{$connection}.{$userTable},username,$id"])->disable();
            $form->text('name', trans('admin.name'))->disable();
            $form->text('aaa', trans('当前推荐人'))->customFormat(function () {
                return AdminUser::find($this->tjr_id)->name ?? '顶级';
            })->disable();
            $form->hidden('tjr')->customFormat(function () {
                return $this->tjr_id;
            });
            $form->hidden('id');


            $form->select('tjr_id', trans('修改推荐人'))//trans('users-leader.fields.tjr_id')
            ->options(function () {
                return AdminUser::whereHas('roles', function ($q) {
                    $q->where('id', 2);
                })->pluck('name', 'id')->toArray();
            })->required();

            if ($id == AdministratorModel::DEFAULT_ID) {
                $form->disableDeleteButton();
            }

            $form->saving(function (Form $form) use ($id) {
                $arrId = queryUsersLowerLevel($id);// 自己下级ID
                $form->deleteInput('tjr');

                if (in_array($form->tjr_id, $arrId)) {
                    return $form->error('推荐人不可为自己的下级');
                }
                if ($form->tjr_id == $id) {
                    return $form->error('推荐人不可是自己');
                }
            })->saved(function (Form $form) {
                // 记录修改推荐人
                $this->RecordSaveTjrId();
            });

        });
    }


    /***
     * 记录修改推荐人
     */
    public function RecordSaveTjrId()
    {
        $data = Request()->all();
        return Tjrlog::create([
            'user_id'    => $data['id'],
            'before_tjr' => $data['tjr'],
            'after_tjr'  => $data['tjr_id'],
        ]);
    }

}
